<?php
/**
 * Created by PhpStorm.
 * User: patrick wu
 * Date: 2020/10/28
 * Time: 10:24
 */
namespace App\Http\Controllers\Live;

use App\Service\Live\OperateService;
use App\Service\Live\ClientService;
use App\Service\Live\AlterService;
use SwooleTW\Http\Websocket\Facades\Room;

class ConnectController extends BaseController
{
    /**
     * client connect event handle
     * @param $websocket
     * @param $request
     */
    public function onConnect($websocket, $request)
    {
        /**
         * join the room
         */
        $websocket->join($this->current_room);

        /**
         * get now room numbers
         */
        $total  =   Room::getClients($this->current_room);

        $user_id    =   $websocket->getUserId();

        /**
         * put new guy in redis visitor set
         */
        $client_service     =   new ClientService();

        $user_info  =   $client_service->setUser($user_id);

        $client_service->userVisit($this->current_room, $user_id);
//        $room_visit_num     =   $client_service->getVisitNum($this->current_room);              //观看人数

        /**
         * broadcast event a new guy come in
         */
        $websocket->to($this->current_room)->emit('clientWelcome', ['type' => self::COME_WELCOME, 'msg' => sprintf(self::WELCOME_DESC[self::COME_WELCOME], ($user_info['nick_name'] ?? ''))]);

        /**
         * broadcast event total number
         */
        $websocket->to($this->current_room)->emit('clientUsers', count($total));

        /**
         * broadcast event total thumbs
         */
        $alter_service  =   new AlterService();
        $total_thumb    =   $alter_service->getThumb($this->current_room);

        $websocket->to($this->current_room)->emit('clientTotalThumb', $total_thumb);

        /**
         * focus result
         */
        $room_id    =   $this->current_room;
        [$focus_flag, $focus_result]    =   $alter_service->ifUserFocus($user_id, $room_id);
        if ($focus_flag) {
            $websocket->emit('clientFocusResult', $focus_result);
        }

        /**
         * if user in block
         */
        if ($client_service->inBlock($this->current_room, $user_id)) {
            $websocket->emit('clientBlock', ['user_id' => $user_id]);
        }

        /**
         *  broadcast event goods and coupons info
         */
        $operate_service    =   new OperateService();

        //goods info
        $goods_info     =   $operate_service->goodsInfoSort($this->current_room);
        $websocket->emit('clientGoods', $goods_info);

        $recommend      =   $operate_service->getRecommend($this->current_room);
        if (!empty($recommend)) {
            $recommend  =   is_array($recommend) ? $recommend : [$recommend];
            $websocket->emit('clientRecommend', $operate_service->goodsInfo($recommend));
        }

    }

    /**
     * client disconnect event handle
     * @param $websocket
     * @param $data
     */
    public function onDisconnect($websocket, $data)
    {
        $user_id    =   $websocket->getUserId();

        $client_service     =   new ClientService();

        $user_info  =   $client_service->setUser($user_id);

        /**
         * broadcast event a guy leave
         */
        $websocket->to($this->current_room)->emit('clientWelcome', ['type' => self::LEAVE_WELCOME, 'msg' => sprintf(self::WELCOME_DESC[self::LEAVE_WELCOME], ($user_info['nick_name'] ?? ''))]);

        /**
         * leave the room
         */
        $websocket->leave($this->current_room);

        /**
         * get now room numbers
         */
        $total  =   Room::getClients($this->current_room);

        /**
         * record user watch line
         */
        $service    =   new ClientService();
        $service->userLeave($this->current_room, $user_id);

        /**
         * broadcast event total number
         */
        $websocket->to($this->current_room)->emit('clientUsers', count($total));

    }

    /**
     * client init data
     * @param $websocket
     * @param $data
     */
    public function onInit($websocket, $data)
    {
        $user_id    =   $websocket->getUserId();

        /**
         * put new guy in redis visitor set
         */
        $client_service     =   new ClientService();

//        $room_watch_number  =   $client_service->getWatchNumber($this->current_room);

        /**
         * broadcast event total number
         */
        $websocket->to($this->current_room)->emit('clientUsers', count(Room::getClients($this->current_room)));

        /**
         * broadcast event total thumbs
         */
        $alter_service  =   new AlterService();
        $total_thumb    =   $alter_service->getThumb($this->current_room);

        $websocket->to($this->current_room)->emit('clientTotalThumb', $total_thumb);

        /**
         * focus result
         */
        $room_id    =   $this->current_room;
        [$focus_flag, $focus_result]    =   $alter_service->ifUserFocus($user_id, $room_id);
        if ($focus_flag) {
            $websocket->emit('clientFocusResult', $focus_result);
        }

        /**
         * if user in block
         */
        if ($client_service->inBlock($this->current_room, $user_id)) {
            echo $user_id . 'is blocking' . PHP_EOL;
            $websocket->emit('clientBlock', ['user_id' => $user_id]);
        }

        /**
         *  broadcast event goods and coupons info
         */
        $operate_service    =   new OperateService();

        //goods info
        $goods_info     =   $operate_service->goodsInfoSort($this->current_room);
        $websocket->emit('clientGoods', $goods_info);

        $recommend      =   $operate_service->getRecommend($this->current_room);
        if (!empty($recommend)) {
            $recommend  =   is_array($recommend) ? $recommend : [$recommend];
            $websocket->emit('clientRecommend', $operate_service->goodsInfo($recommend));
        }

    }

}
